package com.geostar.constructionland.analyse.service.spatiaanalyst;

import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.factory.BlockTypeFactory;
import com.geostar.constructionland.analyse.service.AlyVerService;
import com.geostar.constructionland.common.entity.analyse.AnalyseVersion;
import com.geostar.constructionland.common.global.AlyVersionConstant;
import com.geostar.constructionland.common.global.ConstAnalyseType;
import com.geostar.constructionland.common.service.logs.JsydFxrzService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yelll
 * @since 2020-5-28 14:40
 * 空间分析参数服务
 **/
@Service
@Slf4j
public class SptAlyParamService {
    @Autowired
    AlyVerService alyVerService;

    @Autowired
    private JsydFxrzService jsydFxrzService;

    @Autowired
    private BlockTypeFactory blockTypeFactory;

    public Map<String, Object> getCommonParam(AnalyseModel analyseModel) throws Exception {
        /**获取分析版本信息**/
        AnalyseVersion alyVerion = alyVerService.getAlyVerByAnalyModel(analyseModel);
        /**构造分析请求参数**/
        Map<String, Object> paramMap = new HashMap<>();
        /**设置通用请求参数**/
        paramMap.put("ApplicationGUID", StringUtils.isNotBlank(analyseModel.getApplicationGuid())?analyseModel.getApplicationGuid():alyVerion.getApplicationGuid());
        paramMap.put("AnalysisID", analyseModel.getAlyId());
        paramMap.put("Blocks", blockTypeFactory.getBlockList(analyseModel.getBlockType(),analyseModel.getBlocks()));
        paramMap.put("AnalysisType", analyseModel.getFxlx());
        /**默认返回图形**/
        paramMap.put("SFFHTX", true);
        if (AlyVersionConstant.UNNEEDED.equals(alyVerion.getSffhtx())) {
            //!alyVerion.isSffhtx()
            paramMap.put("SFFHTX", false);
        }
        /**默认不是用返回**/
        paramMap.put("IsXZDWBuffer", false);
        if (AlyVersionConstant.NEEDED.equals(alyVerion.getNeedBuff())) {
            //AalyVerion.isNeedBuff()
            paramMap.put("IsXZDWBuffer", true);
        }

        /**分析类型如果有地类年度和权属年度需要放入设置**/
        String yearMonth = "";
        String yearMonth2 = "";
        String yearMonth3 = "";
        String[] bizStrs = analyseModel.getFxlx().split("_");
        if (bizStrs.length > 1) {
            yearMonth = bizStrs[1];
        }
        if (bizStrs.length > 2) {
            yearMonth2 = bizStrs[2];
        }
        if (bizStrs.length > 3) {
            yearMonth3 = bizStrs[3];
        }

        /**设置地类年度**/
        if (AlyVersionConstant.NEEDED.equals(alyVerion.getNeedDlnd())) {
            //alyVerion.isNeedDlnd()
            paramMap.put("YearMonth", StringUtils.isBlank(yearMonth) ? analyseModel.getDlnd() : yearMonth);
        }
        /**设置耕地年度**/
        if (AlyVersionConstant.NEEDED.equals(alyVerion.getNeedGdnd())) {
            //alyVerion.isNeedGdnd()
            paramMap.put("YearMonth3", StringUtils.isBlank(yearMonth3) ? analyseModel.getGdnd() : yearMonth3);
        }
        /**设置权属年度**/
        if (AlyVersionConstant.NEEDED.equals(alyVerion.getNeedQsnd())) {
            //alyVerion.isNeedQsnd()
            paramMap.put("YearMonth2", StringUtils.isBlank(yearMonth2) ? analyseModel.getQsnd() : yearMonth2);
        }
        /**是否使用吉威平差*/
        if (AlyVersionConstant.NEEDED.equals(alyVerion.getNeedGeoWayAdjust())) {
            paramMap.put("IsAdjustResult", true);
        }
        /** 落图分析需要添加uniqueTaskId **/
        if (ConstAnalyseType.CONST_YZFXLT.equals(analyseModel.getFxlx())) {
            paramMap.put("uniqueTaskId", analyseModel.getUniqueTaskId());
        }
        return paramMap;
    }
}
